package com.higgins.eventtracking.client.health.fundamentals;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.EventTrackingService;
import com.higgins.eventtracking.client.EventTrackingServiceAsync;
import com.higgins.eventtracking.client.dto.GoalDTO;
import com.higgins.eventtracking.client.health.components.WeekNumberListBox;

public class RulesSubTab extends VerticalPanel 		{
	private ScrollPanel achievementsScrollPanel = null;
	private final EventTrackingServiceAsync greetingService = GWT
														.create(EventTrackingService.class);
	private VerticalPanel panel = null;
	private RulesColumns ruleColumns = null;
	private RulesTable ruleTable = null;
	private boolean firstRun = true;
	private String frameOfMind = null;
	private RulesTab parentTab = null;
	
	public RulesSubTab(RulesTab parentTab, String frameOfMind)	{
		this.parentTab = parentTab;
		this.frameOfMind = frameOfMind;
	}
	public void update()	{
		if (firstRun)	
			initialise();
	}
	public void initialise()	{
		clear();
		setHeight("20em");
//		populateGoals();
		ruleColumns = new RulesColumns();
		ruleTable = new RulesTable(ruleColumns);
		ruleTable.setFrameOfMind(frameOfMind);
		panel = new VerticalPanel();
		panel.add(ruleTable);
		achievementsScrollPanel = new ScrollPanel();
		achievementsScrollPanel.add(panel);
		achievementsScrollPanel.setHeight("40em");
		add(achievementsScrollPanel);
//		firstRun = false;
	}

	private void populateGoals()	{
		greetingService.retrieveGoals(frameOfMind, new AsyncCallback<List<GoalDTO>>()	{
			@Override
			public void onFailure(Throwable caught) {
				//parentTab.setSubHeaderCount(frameOfMind, 0);
			}
			@Override
			public void onSuccess(List<GoalDTO> results) {
				addGoals(results);
				//parentTab.setSubHeaderCount(frameOfMind, results.size());
			}
		});		
	}
	
	private void addGoals(List<GoalDTO> results)	{
		try	{
			achievementsScrollPanel.remove(panel);
		} catch (Exception e)	{
			
		}
		panel = new VerticalPanel();
		for (GoalDTO result:results)	{
			Label label = new Label(result.getTask());
			panel.add(label);
			panel.setCellHeight(label, Integer.toString(label.getElement().getOffsetHeight())+"px");
		}
		panel.setHeight(String.valueOf(results.size())+ "em");
		achievementsScrollPanel.add(panel);
		achievementsScrollPanel.setHeight("15em");

	}
}
